package com.mti.epivisitemobile.view
{
	import com.mti.epivisitemobile.AppFacade;
	import com.mti.epivisitemobile.Constants;
	
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class StartViewMediator extends Mediator
	{
		public static const NAME:String = "StartViewMediator";
		
		public function StartViewMediator(viewComponent:StartView)
		{
			super(NAME, viewComponent);
			
			if (AppFacade(facade).appReady == true)
				view.addEventListener(FlexEvent.CREATION_COMPLETE, onViewCreationComplete);
		}
		
		private function onViewCreationComplete(event:FlexEvent):void
		{
			prepareView();
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				Constants.APP_READY,
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case Constants.APP_READY:
					AppFacade(facade).appReady = true;
					prepareView();
					break;
			}
		}
		
		private function prepareView():void
		{
			view.qrcodeButton.addEventListener(MouseEvent.CLICK, onQrcodeButtonClick);
			view.listButton.addEventListener(MouseEvent.CLICK, onListButtonClick);
			view.aboutButton.addEventListener(MouseEvent.CLICK, onAboutButtonClick);
			
			view.setCurrentState("LoadedState");
		}
		
		private function onQrcodeButtonClick(event:MouseEvent):void
		{
			sendNotification(Constants.SHOW_QRCODE);
		}
		
		private function onListButtonClick(event:MouseEvent):void
		{
			sendNotification(Constants.SHOW_EVENT_LIST);
		}
		
		private function onAboutButtonClick(event:MouseEvent):void
		{
			sendNotification(Constants.SHOW_ABOUT);
		}
		
		private function get view():StartView
		{
			return viewComponent as StartView;
		}
	}
}